from typing import List


class Solution:
    def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
        values = [gas[i % len(gas)] - cost[i % len(gas)] for i in range(len(gas) * 2 - 1)]
        left = 0
        right = 0
        sums = 0
        while right < 2 * len(gas) - 1:
            sums += values[right]
            if sums < 0:
                sums = 0
                left = right + 1
                right = left
                continue
            right += 1
            if right - left >= len(gas):
                return left
        return -1


if __name__ == '__main__':
    s = Solution()
    r = s.canCompleteCircuit(gas=[1, 2, 3, 4, 5], cost=[3, 4, 5, 1, 2])
    print(r)
